package com.foreknow.tijian.dao.Impl;

import com.foreknow.tijian.dao.BusinessDao;
import com.foreknow.tijian.po.Business;
import com.foreknow.tijian.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BusinessDaoImpl implements BusinessDao {
    //连接数据库
    Connection con= DBUtil.getConnection();
    PreparedStatement pst;
    @Override
    public List<Business> listBusinessByOrderTypeId(int orderTypeId) {
       String sql="select * from business where orderTypeId=? order by businessId";
        List<Business> businessList = new ArrayList<>();
        try {
            //获取与处理对象
            pst=con.prepareStatement(sql);
            pst.setInt(1, orderTypeId);
            ResultSet rs=pst.executeQuery();//执行查询
            //结果进行遍历
            while(rs.next()){
                Business business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));
                //添加到集合
                businessList.add(business);

            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        finally {
            //关闭资源
            DBUtil.close(pst);
        }
      return businessList;
    }

    @Override
    public Business getBusinessById(int id) throws SQLException {
        String sql="select * from business where businessId=?";
        Business business=new Business();
        try{
            pst=con.prepareStatement(sql);
            //第一个是占位符，第二个是id
            pst.setInt(1,id);
            ResultSet rs=pst.executeQuery();
            if(!rs.next()){
                return null;
            }
            else{
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));
            }
        }
        catch(SQLException e){

        }
        finally {
            DBUtil.close(pst);
        }
        return business;

    }


}
